package demo6;

import java.util.Arrays;
import java.util.Objects;

class Student {
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

//    @Override
//    public int compareTo(Student o) {
//        if (this.age>o.age){
//            return 1;
//        }else if(this.age==o.age){
//            return 0;
//        }else{
//            return -1;
//        }
////        return this.name.compareTo(o.name);
//    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}

class MyValue{
    int val;
}

public class Test {

    public static void swap(MyValue v1,MyValue v2){
        int temp=v1.val;
        v1.val=v2.val;
        v2.val=temp;
    }

    public static void main(String[] args) {
        MyValue myValue1=new MyValue();
        MyValue myValue2=new MyValue();
        myValue1.val=10;
        myValue2.val=20;
        System.out.println(myValue1.val+" "+ myValue2.val);
        swap(myValue1,myValue2);
        System.out.println(myValue1.val+" "+ myValue2.val);
    }

    public static void main5(String[] args) {
        Student student1=new Student("zhangsan",20);
        Student student2=new Student("zhangsan",20);
        System.out.println(student1.equals(student2));
//        System.out.println(student1.hashCode(student2));
        System.out.println(student1.hashCode());
        System.out.println(student2.hashCode());
    }
    public static void main4(String[] args) {
        Student[] students=new Student[3];
        students[0]=new Student("zhangsan",19);
        students[1]=new Student("lisi",9);
        students[2]=new Student("abc",69);
        System.out.println(Arrays.toString(students));
        AgeComparator ageComparator=new AgeComparator();
        Arrays.sort(students,ageComparator);
        System.out.println(Arrays.toString(students));
    }

    public static void main3(String[] args) {
        Student[] students=new Student[3];
        students[0]=new Student("zhangsan",19);
        students[1]=new Student("lisi",9);
        students[2]=new Student("abc",69);
        System.out.println(Arrays.toString(students));
        NameComparartor nameComparartor=new NameComparartor();
        Arrays.sort(students,nameComparartor);
        System.out.println(Arrays.toString(students));
    }

    public static void mysort(Comparable[] comparables){
        for (int i = 0; i < comparables.length-1; i++) {
            for (int j = 0; j < comparables.length-1-i; j++) {
                if(comparables[j].compareTo(comparables[j+1])>0){
                    Comparable temp=comparables[j];
                    comparables[j]=comparables[j+1];
                    comparables[j+1]=temp;
                }
            }
        }
    }

//    public static void main3(String[] args) {
//        Student[] students=new Student[3];
//        students[0]=new Student("zhangsan",19);
//        students[1]=new Student("lisi",9);
//        students[2]=new Student("abc",69);
//        System.out.println(Arrays.toString(students));
//        mysort(students);
//        System.out.println(Arrays.toString(students));
//
//    }

    public static void main2(String[] args) {
        Student[] students=new Student[3];
        students[0]=new Student("zhangsan",19);
        students[1]=new Student("lisi",9);
        students[2]=new Student("abc",69);
        System.out.println(Arrays.toString(students));

        Arrays.sort(students);
        System.out.println(Arrays.toString(students));
    }
//    public static void main1(String[] args) {
//        Student student1=new Student("zhangsan",19);
//        Student student2=new Student("lisi",69);
//        System.out.println(student1.compareTo(student2));
//    }


}
